/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import VO.QuemaVO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.DataSource;
import utiles.SQL.GeneralOperations;
import utiles.SQL.SimpleDataSource;
import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;

/**
 *
 * @author szuniga
 */
public class DaoQuema implements DaoQuemaInterface{

public Vector cargarRutQuema() throws DataBaseException, InternalErrorException, SQLException
     {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        CallableStatement callabledStatement = null;
        ResultSet resultSet;
        Vector listaRutVoluntarios = new Vector();

         try {

            String queryString1 = "CALL cargarRutQuema()";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {
                listaRutVoluntarios = new Vector();

                while (resultSet.next()) {

                    listaRutVoluntarios.add(resultSet.getString("rut"));
                }



            }


        } catch (SQLException e) {
            listaRutVoluntarios = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return listaRutVoluntarios;
        }
    }

public boolean registrarQuema(QuemaVO nueva) throws DataBaseException, InternalErrorException, SQLException {

        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        CallableStatement callabledStatement = null;
        boolean resultado = false;

        try
        {
            String query = "CALL RegistrarQuema(?,?,?,?,?,?,?,?)";
            callabledStatement = connection.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            callabledStatement = connection.prepareCall(query);
            callabledStatement.setString("rutV", nueva.getRutVoluntario());
            callabledStatement.setString("rutS",nueva.getRutSolicitante());
            callabledStatement.setString("nomS",nueva.getNombreSolicitante());
            callabledStatement.setString("apeS",nueva.getApellidosSolicitante());
            callabledStatement.setString("dirS",nueva.getDireccionSolicitante());
            callabledStatement.setString("dirQ",nueva.getDireccionLugarQuema());
            callabledStatement.setString("tipMQ",nueva.getTipoMaterialQuema());
            callabledStatement.setString("hrQ",nueva.getHoraQuema());
            callabledStatement.execute();//se hace la consulta

            if(callabledStatement.getUpdateCount()>0) resultado = true;

        }
        catch (SQLException e)
        {
            resultado = false;
            System.out.println("sql EXCEPTION al registrar la quema --->" + e.getMessage());
            throw new InternalErrorException(e);
        }
        finally
        {
            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return resultado;
        }
    }
public Vector listarQuemas() throws DataBaseException, InternalErrorException, SQLException {
Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        QuemaVO aux = null;

        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL listarQuemas()";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
           

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {

                result = new Vector();

                
                while (resultSet.next()) {

                    aux = new QuemaVO();
                    aux.setIdQuema(resultSet.getInt("idquema"));
                    aux.setCodVoluntario(resultSet.getInt("codVoluntario"));
                    aux.setRutSolicitante(resultSet.getString("rutSolicitante"));
                    aux.setNombreSolicitante(resultSet.getString("nombreSolicitante"));
                    aux.setApellidosSolicitante(resultSet.getString("apellidosSolicitante"));
                    aux.setDireccionSolicitante(resultSet.getString("direccionSolicitante"));
                    aux.setDireccionLugarQuema(resultSet.getString("direccionLugarQuema"));
                    aux.setTipoMaterialQuema(resultSet.getString("tipoMaterialQuema"));
                    aux.setHoraQuema(resultSet.getString("HoraQuema"));

                    result.add(aux);
                }


            }


        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION listarQuemas--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }
}
